//
//  JXLEmoticonToolBar.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/10/7.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit

//ToolBar按钮的枚举类型
enum JXLEmoticonToolBarButtonType: Int {
    //最近
    case recent = 1000
    //默认
    case normal = 1001
    //Emoji
    case emoji = 1002
    //浪小花
    case lxh = 1003
    
    
}

class JXLEmoticonToolBar: UIStackView {

    //纪录上次选中的按钮
    var lastSelectedButton: UIButton?
    
    //定义点击按钮的闭包
    var callBack: ((JXLEmoticonToolBarButtonType)->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: setupUI
    func setupUI() -> Void {
        //设置布局方式
        axis = .horizontal
        //设置子控件的填充方式
        distribution = .fillEqually
        
        //添加子控件
        addChildButton(title: "最近", imageName: "compose_emotion_table_left",buttonType: .recent)
        addChildButton(title: "默认", imageName: "compose_emotion_table_mid",buttonType: .normal)
        addChildButton(title: "Emoji", imageName: "compose_emotion_table_mid",buttonType: .emoji)
        addChildButton(title: "浪小花", imageName: "compose_emotion_table_right",buttonType: .lxh)
        
        
    }
    
    //MARK: 添加子控件
    func addChildButton(title: String, imageName: String, buttonType: JXLEmoticonToolBarButtonType) -> Void {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: #selector (buttonAction(btn:)), for: .touchUpInside)
        //设定button的tag值等于对应的枚举值,把枚举的原始值作为tag值使用
        button.tag = buttonType.rawValue
        button.setTitle(title, for: .normal)
        //设置字体颜色
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: imageName + "_selected"), for: .selected)
        //去掉高亮
        button.adjustsImageWhenHighlighted = false
        
        addArrangedSubview(button)
        
        //默认选中默认表情
        if buttonType == .normal {
            lastSelectedButton?.isSelected = false
            button.isSelected = true
            lastSelectedButton = button
        }
        
    }
    
    // 点击按钮触发的方法
   @objc private func buttonAction(btn: UIButton) -> Void {
    
    //  判断如果点击的按钮和上次选中按钮的一样,那么闭包不执行
    if btn == lastSelectedButton {
        return
    }

    lastSelectedButton?.isSelected = false
    btn.isSelected = true
    lastSelectedButton = btn
    //  根据枚举的原始值初始化枚举
    let type = JXLEmoticonToolBarButtonType(rawValue: btn.tag)!
    //  执行闭包
    callBack?(type)
    }
    
    //  根据组数的索引选中相应的按钮
    func selectedButton(section: Int) -> Void {
        //  使用tag 0 表示取到的是当前视图自己
        //根据tag值取到对应的视图，转成对应的类型
        let button = viewWithTag(1000 + section) as! UIButton
        
        // 如果上次选中的按钮与这次相同，直接返回
        if lastSelectedButton == button {
            return
        }
        
        lastSelectedButton?.isSelected = false
        button.isSelected = true
        lastSelectedButton = button
        
    }


}
